<template>
  <div class="region">
    <div class="content">
      <div class="left">地区:</div>
      <ul>
        <li :class="{ active: activeRef === '' }">全部</li>
        <li
          v-for="item in regionList"
          :key="item.id"
          :class="{ active: activeRef === item.value }"
          @click="changeRegionVal(item.value)"
        >
          {{ item.name }}
        </li>
      </ul>
    </div>
  </div>
</template>
<script lang="ts" name="Region" setup>
// 控制高亮
import { onMounted, ref } from "vue";
import { DictData, Result } from "@/types/types.ts";
import { doFindByDictCode } from "@/api/home";
import { DictCodeEnums } from "@/types/enums.ts";
import { ElMessage } from "element-plus";

let activeRef = ref<string>("");
let regionList = ref<Array<DictData>>([]);

onMounted(() => {
  getLevel();
});

// 获取医院等级字典数据
const getLevel = async () => {
  let result: Result = await doFindByDictCode(DictCodeEnums.BEIJING_REGION);
  if (result.code === 200) {
    regionList.value = result.data;
  } else {
    ElMessage.error("地区字典无数据");
  }
};

const changeRegionVal = (value: string) => {
  activeRef.value = value;
  emits("sendRegion", value);
};

let emits = defineEmits(["sendRegion"]);
</script>

<style lang="scss" scoped>
.region {
  color: #7f7f7f;
  margin-top: 10px;
  .content {
    display: flex;
    .left {
      margin-right: 10px;
      width: 36px;
    }
    ul {
      display: flex;
      flex-wrap: wrap;
      li {
        margin-right: 5px;
        margin-bottom: 10px;
        &.active {
          color: #55a6fe;
        }
      }
      li:hover {
        color: #55a6fe;
        cursor: pointer;
      }
    }
  }
}
</style>
